/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.layers;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import id.duapuluhdua.dakidaki.MainState;
import id.duapuluhdua.dakidaki.common.CommonActivity;

/**
 *
 */
public class TipFrameText extends TipFrame
{
  /** Fields */
  private Paint mText;
  private Rect mRtText;

  private String[] mVecLines = {};

  /**
   * The constructor.
   *
   * @param prefs the application preferences.
   * @param state the application state object.
   * @param title the title of the tip frame.
   */
  public TipFrameText(CommonActivity parent, MainState state, String title)
  {
    super(parent, state, title);

    // Paint properties for the text.
    mText = new Paint();
    mText.setAntiAlias(true);
    mText.setColor(0xff442200);
    mText.setStyle(Paint.Style.FILL);
    mText.setTextSize(toPixel(14));
    mText.setTextAlign(Paint.Align.CENTER);

    mRtText = new Rect(0, 0, 0, 0);
  }

  /**
   * Write a text under the title string. If the text contains multiple lines,
   * separate them with a newline symbol ("\n").
   *
   * @param text string text.
   */
  public void setText(String text)
  {
    if (text == null) {
      mVecLines = new String[0];
      return;
    }

    if (text.trim().length() == 0) {
      mVecLines = new String[0];
    }
    else {
      mVecLines = text.split("\n");
    }
  }

  /**
   * Get the total height of the frame, including the text height.
   */
  @Override
  protected int getHeight()
  {
    int value = super.getHeight();

    final int linesCount = mVecLines.length;
    mText.getTextBounds(DUMMY_TEXT, 0, DUMMY_TEXT.length(), mRtText); // for sampling the text height.

    if (linesCount > 0) {
      value += (mRtText.height() + LINE_HEIGHT) * linesCount;
      value -= LINE_HEIGHT;
      value += PADDING;
    }

    return value;
  }

  /**
   * Draw the text after drawing the title string.
   */
  @Override
  public void doDraw(Canvas canvas, Rect area)
  {
    super.doDraw(canvas, area);

    // Draw the text
    int textPosY = getTitleBottom() + mRtText.height();
    for (int i = 0; i < mVecLines.length; i++) {
      canvas.drawText(mVecLines[i], mRtShape.centerX(), textPosY, mText);
      textPosY += mRtText.height() + LINE_HEIGHT;
    }
  }
}
